package prototype.controller.dictionary;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import prototype.constant.UrlConst;
import prototype.controller.AbstractController;
import prototype.data.Dictionary;
import prototype.model.DictionaryModel;

import javax.persistence.EntityManager;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Просмотр словаря
 */
@WebServlet("/" + UrlConst.URL_DICTIONARY_LIST)
public class DictionaryListController extends AbstractController {

	public static final String URL = UrlConst.URL_DICTIONARY_LIST;

	@Override
	protected JsonObject execute(EntityManager entityManager, HttpSession session, JsonElement requestData) throws Exception {
		final JsonObject jsonObject = new JsonObject();
		final JsonArray jsonArray = new JsonArray();
		final List<Dictionary> list = DictionaryModel.list(entityManager);
		for (final Dictionary item : list) {
			jsonArray.add(item.toJsonObject());
		}
		jsonObject.add("dictionaryList", jsonArray);
		return jsonObject;
	}
}
